package cn.cyl.org.controller;

import cn.cyl.basic.util.AjaxResult;
import cn.cyl.basic.util.PageList;
import cn.cyl.org.domain.Department;
import cn.cyl.org.query.DepartmentQuery;
import cn.cyl.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Api(value = "部门接口类",description = "一些CRUD操作")
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 接口：查所有
     * @return
     */
    @ApiOperation(value = "接口：查所有")
    @GetMapping
    public List<Department> findAll(){
       return departmentService.loadAll();
    }

    /**
     * 接口：查一个
     * @param id
     * @return
     */
    @ApiOperation(value = "接口：查一个")
    @GetMapping("/{id}")
    public Department findOne(@PathVariable("id") Long id){
        return departmentService.loadOne(id);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @ApiOperation(value = "接口：删除")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * @param department
     * @return
     */
    @ApiOperation(value = "接口：添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId()==null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：高级和分页
     * @param departmentQuery
     * @return
     */
    @ApiOperation(value = "接口：高级和分页")
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);

    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "接口：批量删除")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除操作失败");
        }
    }

    /**
     * 查询部门树
     * @return
     */
    @GetMapping("/deptTree")
    public List<Department> deptTree(){
       return departmentService.deptTree();
    }


}
